﻿using dz.util.core;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;


namespace ufida.u9.cust.dz.beplugin.Utils
{
    /// <summary>
    /// http请求工具类
    /// </summary>
    public class HttpUtil
    {
        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public async Task<WebResult> Post(string url, string data)
        {
            return await Post(url, data, "application/json");
        }
        public async Task<WebResult> Post(string url, string data,string mediaType)
        {
            Dictionary<string, string> header = new Dictionary<string, string>();

            return await Post(url, data, header, mediaType);
        }


        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="header"></param>
        /// <returns></returns>
        public async Task<WebResult> Post(string url, string data, Dictionary<string, string> header)
        {

            return await Post(url, data, header, "application/json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="header"></param>
        /// <param name="mediaType">application/x-www-form-urlencoded
        /// application/json</param>
        /// <returns></returns>
        public async Task<WebResult> Post(string url, string data, Dictionary<string, string> header, string mediaType)
        {

            WebResult result = new WebResult();

            try
            {
                HttpClient client = HttpService.Instance.HttpClientFactory.CreateClient();                

                var content = new StringContent(data, Encoding.UTF8, mediaType);
                var request = new HttpRequestMessage(HttpMethod.Post, url)
                {
                    Content = content
                };

                if (header != null)
                {
                    foreach (var item in header)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }


                var response = await client.SendAsync(request).ConfigureAwait(false);
                response.EnsureSuccessStatusCode();
                string msg = await response.Content.ReadAsStringAsync().ConfigureAwait(false);


                result.Sucess = true;
                result.Data = msg;
            }
            catch (Exception ex)
            {
               // LogUtil.Instance.Error(ex.Message);
                result.Data = ex.Message;
            }

            return result;
        }

        public async Task<WebResult> Get(string url, FormUrlEncodedContent content)
        {
            WebResult result = new WebResult();
            try
            {
                HttpClient client = HttpService.Instance.HttpClientFactory.CreateClient();

                string urlWithQuery = $"{url}?{content.ReadAsStringAsync().Result}";


                using (var request = new HttpRequestMessage(HttpMethod.Get, urlWithQuery))
                {


                    // 发送GET请求
                    HttpResponseMessage response = await client.SendAsync(request);

                    // 确保响应状态为成功
                    response.EnsureSuccessStatusCode();

                    // 读取响应内容
                    string responseBody = await response.Content.ReadAsStringAsync();

                    //LogUtil.Instance.Debug(responseBody);

                    result.Sucess = true;
                    result.Data = responseBody;
                }
            }
            catch (Exception ex)
            {
                //LogUtil.Instance.Error(ex.Message);
                result.Data = ex.Message;
            }

            return result;
        }

    }
}
